﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class DanhSachSachBLL : BaseBLL
    {
        DAL.DauSachDAO _DauSachDAO;

        public DanhSachSachBLL()
        {
            _DauSachDAO = new DAL.DauSachDAO();
        }

        public int CountMatchFilter(String[] filter)
        {
            return _DauSachDAO.CountMatchFilter(filter);
        }

        public List<DTO.DauSachDTO> MatchFilterPerPage(int page, String[] filter)
        {
            return _DauSachDAO.MatchFilterPerPage(page, filter);
        }

        public int UpdateList(List<DTO.DauSachDTO> dtos)
        {
            int r = -1;
            try
            {
                System.Data.SqlClient.SqlConnection connection = OpenConnection();
                System.Data.SqlClient.SqlTransaction transaction = BeginTransaction();

                foreach (DTO.DauSachDTO dto in dtos)
                {
                    r = UpdateSach(connection, transaction, dto);
                }
                Commit();
                CloseConnection();
                return r;
            }
            catch (System.Exception ex)
            {
                Rollback();
                CloseConnection();
                throw new System.Exception("UpdateList failed " + ex.Message);
            }
            finally
            {
                CloseConnection();
            }           
        }

        public int UpdateSach(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            DTO.DauSachDTO dto)
        {
            return _DauSachDAO.Update(connection,transaction,dto);
        }

        public DTO.DauSachDTO GetSachByID(int IDSach)
        {
            DTO.DauSachDTO dto = new DTO.DauSachDTO();
            dto.ID = IDSach;
            _DauSachDAO.Select(null, null, ref dto);
            return dto;
        }

    }
}
